


public class StringTest {
    public static boolean isEqual(char c1,char c2){
        if(c1=='#'||c2=='#') return true;
        if(c1==c2) return true;
        return false;
    }

    static boolean dealAsteriskSingle(String s1,String s2){
        int len_s1 = s1.length();
        int len_s2 = s2.length();
        int ak_first = s1.indexOf("*");
        int ak_last = s1.lastIndexOf("*");
        if(len_s2<ak_first) return false;
        if(dealNoAsterisk(s1.substring(0,ak_first),s2.substring(0,ak_first))==false) return false;
        if(len_s2<(len_s1-1-ak_last)) return false;
        if(ak_last+1==len_s1&&(len_s2-ak_first)>0) return true;
        if(ak_last+1==len_s1&&(len_s2-ak_first)==0) return false;
        if(len_s1-1-ak_last==len_s2-ak_first) return false;
        if(dealNoAsterisk(s1.substring(ak_last+1,len_s1),s2.substring(len_s2-(len_s1-ak_last-1),len_s2))==false) return false;
        return true;
    }

    static boolean dealAsteriskBoth(String s1,String s2){
        int len_s1 = s1.length();
        int len_s2 = s2.length();
        int ak1_first =s1.indexOf("*");
        int ak1_last = s1.lastIndexOf("*");
        int ak2_first =s2.indexOf("*");
        int ak2_last = s2.lastIndexOf("*");
        int min_ak_first = (ak1_first<ak2_first)?ak1_first:ak2_first;
        if(dealNoAsterisk(s1.substring(0,min_ak_first),s2.substring(0,min_ak_first))==false) return false;
        int ak1_last_restNum = len_s1 - ak1_last -1;
        int ak2_last_restNum = len_s2 - ak2_last -1;
        if(ak1_last_restNum<ak2_last_restNum){
            return (dealNoAsterisk(s1.substring(ak1_last+1,len_s1),s2.substring(len_s2-ak1_last_restNum,len_s2)));
        }
        else{
            return (dealNoAsterisk(s2.substring(ak2_last+1,len_s2),s1.substring(len_s1-ak2_last_restNum,len_s1)));
        }
    }

    static boolean dealNoAsterisk(String s1,String s2){
        if(s1.length()!=s2.length()) return false;
        for(int i=0;i<s1.length();i++){
            if(isEqual(s1.charAt(i),s2.charAt(i))==false) return false;
        }
        return true;
    }

    static boolean isMatched(String s1,String s2){
        int ak1 = s1.indexOf("*");
        int ak2 = s2.indexOf("*");
        if(ak1==-1&&ak2==-1){
            return dealNoAsterisk(s1,s2);
        }
        if(ak1!=-1&&ak2!=-1){
            return dealAsteriskBoth(s1,s2);
        }
        if(ak1==-1&&ak2!=-1){
            return dealAsteriskSingle(s2,s1);
        }
        if(ak1!=-1&&ak2==-1){
            return dealAsteriskSingle(s1,s2);
        }
        return false;
    }
    public static void main(String[] args) {
        String s1="ab*fghijkm*pq";
        String s2="abcd*ij*opjq";
        System.out.println(isMatched(s1,s2));
    }
}
